<?php
class Tag extends AppModel {

	var $name = 'Tag';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasAndBelongsToMany = array(
			'Post' =>
				array('className' => 'Post',
						'joinTable' => 'posts_tags',
						'foreignKey' => 'tag_id',
						'associationForeignKey' => 'post_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'uniq' => '',
						'finderQuery' => '
SELECT User.login, User.name, Category.name, Category.description, Post.id, Post.title, Post.replies, Post.closed, Post.created
FROM posts AS Post
JOIN posts_tags ON posts_tags.tag_id = {$__cakeID__$}  AND posts_tags.post_id = Post.id
LEFT JOIN users AS User ON Post.user_id = User.id
LEFT JOIN categories AS Category ON Post.category_id = Category.id
						',
						'deleteQuery' => '',
						'insertQuery' => ''
				),

	);

        function parseTags($tagString){
        $tags = explode(",",trim($tagString));
        foreach($tags as $tag){
                        if(!empty($tag)){
												$tag=strip_tags(trim($tag));
                        $this->unbindModel(array('hasAndBelongsToMany'=>array('Post')));
                        $tagRow = $this->findByTag($tag);
                        if(is_array($tagRow)){
                                // You can do $this->create() here
                                $ids[] = $tagRow['Tag']['id'];
                        }else{
                                //Code to insert the new tag
                                $newTag['Tag']['tag']=$tag;
                                $newTag['Tag']['id']="";
                                $this->save($newTag);
                                $ids[]=$this->getLastInsertId();
                        }
                        }
        }
        return $ids;
    }

	function topTags($l=null) {
		$q="SELECT tags. * , count( posts_tags.tag_id ) postCount
                            FROM tags tags
                            LEFT JOIN posts_tags posts_tags ON tags.id = posts_tags.tag_id
                            GROUP BY tags.id
                            ORDER BY postCount DESC";
		if($l) $q.=" LIMIT ".$l;
		return $this->query($q);
	}

	function findPostsByTag($tagid,$fields,$criteria,$page) {
		$limit=PAGESIZE;
		$offset = $limit * ($page-1);
		$q="SELECT $fields
FROM posts AS Post
JOIN posts_tags ON posts_tags.tag_id = $tagid  AND posts_tags.post_id = Post.id
LEFT JOIN users AS User ON Post.user_id = User.id
LEFT JOIN categories AS Category ON Post.category_id = Category.id
ORDER BY $criteria
LIMIT $offset, $limit";
		return $this->query($q);
	}
    function findCountByTag($tagid) {
		$q="SELECT COUNT(Post.id) AS count
FROM posts AS Post
JOIN posts_tags ON posts_tags.tag_id = $tagid  AND posts_tags.post_id = Post.id";
		$count=$this->query($q);
		return $count[0][0]['count'];
	}


}
?>